<!--
@license
Copyright (C) 2017 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/iron-autogrow-textarea/iron-autogrow-textarea.html">
<link rel="import" href="../../../bower_components/iron-input/iron-input.html">

<link rel="import" href="../../plugins/gr-endpoint-decorator/gr-endpoint-decorator.html">
<link rel="import" href="../../shared/gr-download-commands/gr-download-commands.html">
<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
<link rel="import" href="../../shared/gr-select/gr-select.html">
<link rel="import" href="../../../styles/gr-form-styles.html">
<link rel="import" href="../../../styles/gr-subpage-styles.html">
<link rel="import" href="../../../styles/shared-styles.html">


<dom-module id="gr-repo">
  <template>
    <style include="shared-styles"></style>
    <style include="gr-subpage-styles">
      h2.edited:after {
        color: var(--deemphasized-text-color);
        content: ' *';
      }
      .loading,
      .hideDownload {
        display: none;
      }
      #loading.loading {
        display: block;
      }
      #loading:not(.loading) {
        display: none;
      }
      .repositorySettings {
        display: none;
      }
      .repositorySettings.showConfig {
        display: block;
      }
    </style>
    <style include="gr-form-styles"></style>
    <main class="gr-form-styles read-only">
      <style include="shared-styles"></style>
      <div class="info">
        <h1 id="Title" class$="name">
          [[repo]]
          <hr/>
        </h1>
        <div>
          <a href$="[[_computeChangesUrl(repo)]]">查看提交记录</a>
        </div>
      </div>
      <div id="loading" class$="[[_computeLoadingClass(_loading)]]">加载中...</div>
      <div id="loadedContent" class$="[[_computeLoadingClass(_loading)]]">
        <div id="downloadContent" class$="[[_computeDownloadClass(_schemes)]]">
          <h2 id="download">仓库下载</h2>
          <fieldset>
            <gr-download-commands
                id="downloadCommands"
                commands="[[_computeCommands(repo, _schemesObj, _selectedScheme)]]"
                schemes="[[_schemes]]"
                selected-scheme="{{_selectedScheme}}"></gr-download-commands>
          </fieldset>
        </div>
        <h2 id="configurations"
            class$="[[_computeHeaderClass(_configChanged)]]">仓库配置</h2>
        <div id="form">
          <fieldset>
            <h3 id="Description">仓库介绍</h3>
            <fieldset>
              <iron-autogrow-textarea
                  id="descriptionInput"
                  class="description"
                  autocomplete="on"
                  placeholder="<在这里插入仓库介绍>"
                  bind-value="{{_repoConfig.description}}"
                  disabled$="[[_readOnly]]"></iron-autogrow-textarea>
            </fieldset>
            <h3 id="Options">其他选项</h3>
            <fieldset id="options">
              <section>
                <span class="title">状态</span>
                <span class="value">
                  <gr-select
                      id="stateSelect"
                      bind-value="{{_repoConfig.state}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat" items=[[_states]]>
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">提交类型</span>
                <span class="value">
                  <gr-select
                      id="submitTypeSelect"
                      bind-value="{{_repoConfig.submit_type}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatSubmitTypeSelect(_repoConfig)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">允许合并</span>
                <span class="value">
                  <gr-select
                      id="contentMergeSelect"
                      bind-value="{{_repoConfig.use_content_merge.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.use_content_merge)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">
                  不在目标分支中的每次提交都创建一个新的更改
                </span>
                <span class="value">
                  <gr-select
                      id="newChangeSelect"
                      bind-value="{{_repoConfig.create_new_change_for_all_not_in_target.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.create_new_change_for_all_not_in_target)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">需要在提交信息中包含 Change-Id</span>
                <span class="value">
                  <gr-select
                      id="requireChangeIdSelect"
                      bind-value="{{_repoConfig.require_change_id.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.require_change_id)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section
                   id="enableSignedPushSettings"
                   class$="repositorySettings [[_computeRepositoriesClass(_repoConfig.enable_signed_push)]]">
                <span class="title">启用推送签名</span>
                <span class="value">
                  <gr-select
                      id="enableSignedPush"
                      bind-value="{{_repoConfig.enable_signed_push.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.enable_signed_push)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section
                   id="requireSignedPushSettings"
                   class$="repositorySettings [[_computeRepositoriesClass(_repoConfig.require_signed_push)]]">
                <span class="title">需要签名推送</span>
                <span class="value">
                  <gr-select
                      id="requireSignedPush"
                      bind-value="{{_repoConfig.require_signed_push.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.require_signed_push)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">
                  当需要审查提交的修改时，不允许隐式合并</span>
                <span class="value">
                  <gr-select
                      id="rejectImplicitMergesSelect"
                      bind-value="{{_repoConfig.reject_implicit_merges.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.reject_implicit_merges)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section id="noteDbSettings" class$="repositorySettings [[_computeRepositoriesClass(_noteDbEnabled)]]">
                <span class="title">
                  允许添加未注册用户作为提交的审阅者和CCs</span>
                <span class="value">
                  <gr-select
                      id="unRegisteredCcSelect"
                      bind-value="{{_repoConfig.enable_reviewer_by_email.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.enable_reviewer_by_email)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                  </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">
                  默认情况下将所有新的提交设置为私有</span>
                <span class="value">
                  <gr-select
                      id="setAllnewChangesPrivateByDefaultSelect"
                      bind-value="{{_repoConfig.private_by_default.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.private_by_default)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">
                  默认情况下设置新的提交为"work in progress"</span>
                <span class="value">
                  <gr-select
                      id="setAllNewChangesWorkInProgressByDefaultSelect"
                      bind-value="{{_repoConfig.work_in_progress_by_default.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.work_in_progress_by_default)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">最大Git对象大小限制</span>
                <span class="value">
                  <input
                      id="maxGitObjSizeInput"
                      bind-value="{{_repoConfig.max_object_size_limit.configured_value}}"
                      is="iron-input"
                      type="text"
                      disabled$="[[_readOnly]]">
                  <template is="dom-if" if="[[_repoConfig.max_object_size_limit.value]]">
                    effective: [[_repoConfig.max_object_size_limit.value]] bytes
                  </template>
                </span>
              </section>
              <section>
                <span class="title">提交时将创建日期与提交日期匹配</span>
                <span class="value">
                  <gr-select
                      id="matchAuthoredDateWithCommitterDateSelect"
                      bind-value="{{_repoConfig.match_author_to_committer_date.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.match_author_to_committer_date)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                  </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">拒绝空的提交</span>
                <span class="value">
                  <gr-select
                      id="rejectEmptyCommitSelect"
                      bind-value="{{_repoConfig.reject_empty_commit.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                                items="[[_formatBooleanSelect(_repoConfig.reject_empty_commit)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                  </select>
                  </gr-select>
                </span>
              </section>
            </fieldset>
            <h3 id="Options">贡献者协议</h3>
            <fieldset id="agreements">
              <section>
                <span class="title">
                  要求上传有效的贡献者协议</span>
                <span class="value">
                  <gr-select
                      id="contributorAgreementSelect"
                      bind-value="{{_repoConfig.use_contributor_agreements.configured_value}}">
                  <select disabled$="[[_readOnly]]">
                    <template is="dom-repeat"
                        items="[[_formatBooleanSelect(_repoConfig.use_contributor_agreements)]]">
                      <option value="[[item.value]]">[[item.label]]</option>
                    </template>
                    </select>
                  </gr-select>
                </span>
              </section>
              <section>
                <span class="title">需要在提交消息中签名</span>
                <span class="value">
                  <gr-select
                        id="useSignedOffBySelect"
                        bind-value="{{_repoConfig.use_signed_off_by.configured_value}}">
                    <select disabled$="[[_readOnly]]">
                      <template is="dom-repeat"
                          items="[[_formatBooleanSelect(_repoConfig.use_signed_off_by)]]">
                        <option value="[[item.value]]">[[item.label]]</option>
                      </template>
                    </select>
                  </gr-select>
                </span>
              </section>
            </fieldset>
            <!-- TODO @beckysiegel add plugin config widgets -->
            <gr-button
                on-tap="_handleSaveRepoConfig"
                disabled$="[[_computeButtonDisabled(_readOnly, _configChanged)]]">保存修改</gr-button>
          </fieldset>
          <gr-endpoint-decorator name="repo-config">
            <gr-endpoint-param name="repoName" value="[[repo]]"></gr-endpoint-param>
          </gr-endpoint-decorator>
        </div>
      </div>
    </main>
    <gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
  </template>
  <script src="gr-repo.js"></script>
</dom-module>
